package cn.edu.nchu.fragmentdemo.fragments;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.nchu.fragmentdemo.MainActivity;
import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.adapter.RouteTaskAdapter;
import cn.edu.nchu.fragmentdemo.adapter.WayPointAdapter;
import cn.edu.nchu.fragmentdemo.bean.RouteTask;
import cn.edu.nchu.fragmentdemo.bean.WayPoint;
import cn.edu.nchu.fragmentdemo.customcomponents.UavCustomSeekbarWithCheckbox;
import cn.edu.nchu.fragmentdemo.db.dao.RouteTaskDao;
import cn.edu.nchu.fragmentdemo.db.dao.WayPointDao;
import cn.edu.nchu.fragmentdemo.db.helper.ORMLiteHelper;
import cn.edu.nchu.fragmentdemo.db.server.RouteTaskDataServer;
import cn.edu.nchu.fragmentdemo.db.server.WayPointServer;
import lombok.SneakyThrows;

/**
 * @className: UavWayPointSettingFragment
 * @description: TODO 类描述
 * @author: Kuangkeli
 * @date: 2023/10/08 19:53
 **/
public class UavWayPointSettingFragment extends Fragment implements WayPointAdapter.OnButtonClickListener{
    /**
     * 航点Dao
     */
    Dao<WayPoint, Integer> wayPointDao;
    private List<WayPoint> wayPointList;
    /**
     * 航点适配器
     */
    private WayPointAdapter wayPointAdapter;

    private RecyclerView waypoint_select;

    private MainActivity mainActivity;

    private int selectedPosition = -1; // 默认没有选中任何按钮

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_uav_waypoint_setting, container, false);
        mainActivity = (MainActivity)MainActivity.getActivity();
        waypoint_select=rootView.findViewById(R.id.waypoint_select);

        wayPointDao = WayPointDao.getWayPointDao(getActivity()); // 初始化wayPointDao
        Init();
        return rootView;
    }

    public void Init(){
        Bundle bundle = getArguments();//获取航线id
        Integer routeId=-1;
        if (bundle != null) {
            routeId= bundle.getInt("routeId");
            System.out.println("routeId = " + routeId);

        }
        wayPointList = WayPointServer.getWaypointList(routeId,wayPointDao);
        System.out.println("wayPointList = " + wayPointList);
        if(wayPointList.isEmpty())
        {
            // 隐藏所有控件
            View viewsToHide = rootView.findViewById(R.id.sidebar_content);

            viewsToHide.setVisibility(View.GONE);


            // 显示无航点信息
            TextView textView = rootView.findViewById(R.id.empty_text);
            textView.setVisibility(View.VISIBLE);
            textView.setText("当前无航点信息");

        }
        else
        {

            wayPointAdapter = new WayPointAdapter(getActivity(), wayPointList,this,1); // 航点数据列表
            waypoint_select.setAdapter(wayPointAdapter);

        }

    }


    @Override
    public void onButtonClick(int position) {
        try {
            WayPoint wayPoint = wayPointDao.queryForId(position + 1); // 假设position从1开始，对应数据库中的id
            // 根据wayPoint对象获取航点的各个属性值
            Double flightSpeed = wayPoint.getFlight_speed();
            Integer hoverTime = wayPoint.getHover_time();
            Double ascentSpeed = wayPoint.getAscent_speed();
            Double descentSpeed = wayPoint.getDescent_speed();
            Double longitude = wayPoint.getWayPointLongitude();
            Double latitude = wayPoint.getWayPointLatitude();
            // 找到flightSpeed的seekbarWithCheckBox，并将其进度设置为flight_speed的值
            UavCustomSeekbarWithCheckbox flightSpeedSeekbar = rootView.findViewById(R.id.flightSpeed);
            flightSpeedSeekbar.setProgressValue((int) Math.round(flightSpeed));
            UavCustomSeekbarWithCheckbox hoverTimeSeekbar = rootView.findViewById(R.id.hover_time);
            hoverTimeSeekbar.setProgressValue(hoverTime);
            UavCustomSeekbarWithCheckbox ascentSpeedSeekbar = rootView.findViewById(R.id.ascent_speed);
            ascentSpeedSeekbar.setProgressValue((int) Math.round(ascentSpeed)*10);
            UavCustomSeekbarWithCheckbox descentSpeedSeekbar = rootView.findViewById(R.id.descent_speed);
            descentSpeedSeekbar.setProgressValue((int) Math.round(descentSpeed)*10);
            EditText longitudeText=rootView.findViewById(R.id.waypoint_longitude);
            longitudeText.setText(""+longitude);
            EditText latitudeText=rootView.findViewById(R.id.waypoint_latitude);
            latitudeText.setText(""+latitude);

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }





}
//        WayPoint new1 =  new WayPoint(7,20,5.0,0,0,2.0,2.0,118.8149215,32.0302582);
//        WayPoint new2 =  new WayPoint(7,20,5.0,0,0,2.0,2.0,118.8149215,32.0302582);
//        wayPointList.add(new1);
//        wayPointList.add(new2);
//        wayPointList.add(new2);
//        wayPointList.add(new2);
//        wayPointList.add(new2);
//        wayPointList.add(new2);
//        wayPointList.add(new2);
//        wayPointList.add(new2);
//        wayPointList.add(new2);
//        wayPointList.add(new2);
//        wayPointList.add(new2);